package com.craney.aboutSound
{
	import com.craney.util.ControlProgressBar;
	import com.craney.util.ISoundPlay;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	public class PlaySound extends Sprite implements ISoundPlay
	{
		//播放时间文字
		private var tf:TextField = new TextField();
		
		//声音
		private var mySound:Sound = new Sound();
		
		private var urlString:String = "";
		
		private var url:URLRequest = new URLRequest(urlString);
		
		private var progressBar:ControlProgressBar = new ControlProgressBar(150, 10, 0xCCFF00, 0xCCCCFF);
			
		private var channel:SoundChannel;
		//更新播放进度计时器
		private var controlTimer:Timer = new Timer(50);
		//声音相关控制
		private var context:SoundLoaderContext = new SoundLoaderContext();
		//总时间
		private var totalTime:Number;
		
		public function PlaySound(urlString:String)
		{
			//初始化地址
			this.urlString = urlString;
			this.url.url = urlString;
			
			init(); 
		}
		
		private function init():void
		{
			initDownload();
			initBar();
			initTextField();
			
			//播放音乐
			playFrom(0);
			
			//计算歌曲总长度监听
			addEventListener(Event.ENTER_FRAME, calculateHandler);
		}
		
		//初始化并开始下载
		private function initDownload():void
		{
			mySound.addEventListener(IOErrorEvent.IO_ERROR, ioErrHandler);
			mySound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			mySound.addEventListener(Event.COMPLETE, function(event:Event):void{trace("~~~~~~~~~~~~~", mySound.length);});
			//载入参数
			context.checkPolicyFile = true;
			//缓冲 3 seconds
			context.bufferTime = 3000;
			//载入音乐
			mySound.load(url, context);
			
		}
		
		//初始化控制条
		private function initBar():void
		{
			//播放控制监听
			controlTimer.addEventListener(TimerEvent.TIMER, controlProgressHandler);
			
			//选择播放监听
			progressBar.addClickControl(this);
			addChild(progressBar);
		}
		
		//初始化播放状态文本框
		private function initTextField():void
		{
			//文字
			tf.border = true;
			tf.autoSize = TextFieldAutoSize.CENTER;
			tf.y = 50;
			tf.text = "缓冲中...请稍后...";
			addChild(tf);
		}
		
		private function calculateHandler(event:Event):void
		{
			//停止缓冲,开始播放
			if(mySound.isBuffering==false)
			{
				//计算总时间
				totalTime = mySound.length / mySound.bytesLoaded * mySound.bytesTotal;
				//停止监听
				removeEventListener(Event.ENTER_FRAME, calculateHandler);
				//开始进度条更新
				controlTimer.start();
			}
		}
		
		private function controlProgressHandler(event:TimerEvent):void
		{
			progressBar.drawController(channel.position/totalTime);
			
			tf.text = (channel.position/1000).toFixed(2);
			tf.appendText("\t/\t");
			tf.appendText((totalTime/1000).toFixed(2));
			
		}
		
		private function soundEndHandler(event:Event):void
		{
			controlTimer.stop();
			controlTimer.reset();
		}
		
		private function ioErrHandler(event:IOErrorEvent):void
		{
			trace(event.toString());
			trace("Can not find the destination sound!");
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
			progressBar.drawProgress(event.bytesLoaded/event.bytesTotal);
			trace(mySound.length);
		}
		
		//从指定百分比处开始播放
		public function playFrom(percentage:Number=0):void
		{
			//停止当前相道
			if(channel!=null)
			{
				channel.stop();
				channel.removeEventListener(Event.SOUND_COMPLETE, soundEndHandler);
			}
			
			//重新播放并给Channel赋值
			channel = mySound.play(percentage*mySound.length/mySound.bytesLoaded*mySound.bytesTotal);
			//声音结束监听
			channel.addEventListener(Event.SOUND_COMPLETE, soundEndHandler);

		}
		
	}
}